<?php
/**
 * 节点管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: NodesController.class.php v1.0 2014-06-12 17:27:03 $
 */
namespace Admin\Controller;
class NodesController extends CommonController{

    private $model;
    private $service;
    
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Node");
        $this->service = D("Node", 'Service');
    }
    /**
     * 节点列表
     * @return
     */
    public function index() {
        $nodes = $this->service->getNodes();

        foreach ($nodes as $key => $node) {
            $nodes[$key]['type'] = $this->service->getNodeType($node['level']);
        }

        $this->assign('nodes', $nodes);
        $this->assign('rows_count', count($nodes));
        $this->display();
    }

    public function add() {
        //$group = $this->model->getGroupNodes(array("pid" => 0));
        //var_dump($group);die;
        $nodes = $this->service->getNodes();
        foreach ($nodes as $key => $node) {
            $nodes[$key]['type'] = $this->service->getNodeType($node['level']);
        }
        $this->assign('nodes', $nodes);
        $this->display();
    }
    
    public function create() {
        $model = I("post.model");
        $auto = I("post.auto", 0, "intval") === 1 ? true : false;
        if (!isArrayAndNotEmpty($model)) {
            return $this->errorReturn('无效的操作！');
        }
        $result = $this->service->addModuleNodes($model['title'], $model['name'], $model['pid'], $auto);
        if (!$result['status']) {
            return $this->errorReturn($result['data']['error']);
        }
        
        return $this->successReturn('添加节点成功！', U('index'));
    }
    /**
     * 切换节点状态
     * @return
     */
    public function toggleStatus() {
        $nodeService = D('Node', 'Service');
        if (!isset($_GET['id'])
            || !$nodeService->existNode($_GET['id'])) {
            return $this->errorReturn('无效的操作！');
        }

        if (!$_GET['status']) {
            $nodeService->setStatus($_GET['id'], 1);
        } else {
            $nodeService->setStatus($_GET['id'], 0);
        }

        $info = $_GET['status'] ? '禁用成功！' : '启用成功！';
        $this->successReturn($info);
    }
}
